<?php
/**
 * 修复重复的权限管理菜单
 */

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 修复重复的权限管理菜单 ===\n\n";
    
    // 删除重复的一级"权限管理"菜单（ID:42）
    $stmt = $pdo->prepare("DELETE FROM system_menu WHERE id = 42");
    $stmt->execute();
    echo "✓ 已删除重复的一级菜单（ID:42）\n\n";
    
    // 将"权限管理"（ID:9）从"系统管理"下提升为一级菜单
    echo "选项1: 将现有的权限管理提升为一级菜单\n";
    echo "选项2: 保持现状（权限管理在系统管理下）\n\n";

    // 这里我们选择选项1，将权限管理提升为一级菜单
    $stmt = $pdo->prepare("UPDATE system_menu SET pid = 0, sort = 40 WHERE id = 9");
    $stmt->execute();
    echo "✓ 已将权限管理（ID:9）提升为一级菜单\n\n";
    
    echo "=== 当前菜单结构 ===\n\n";
    
    // 显示所有一级菜单及其子菜单
    $stmt = $pdo->query("SELECT id, title, sort FROM system_menu WHERE pid = 0 AND status = 1 ORDER BY sort DESC, id ASC");
    $menus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($menus as $menu) {
        echo "【{$menu['title']}】(ID:{$menu['id']}, 排序:{$menu['sort']})\n";
        
        // 显示二级菜单
        $stmt2 = $pdo->prepare("SELECT id, title, node FROM system_menu WHERE pid = ? AND status = 1 ORDER BY sort DESC, id ASC");
        $stmt2->execute([$menu['id']]);
        $subMenus = $stmt2->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($subMenus as $sub) {
            echo "  └─ {$sub['title']} ({$sub['node']})\n";
            
            // 显示三级菜单
            $stmt3 = $pdo->prepare("SELECT id, title, node FROM system_menu WHERE pid = ? AND status = 1 ORDER BY sort DESC, id ASC");
            $stmt3->execute([$sub['id']]);
            $subSubMenus = $stmt3->fetchAll(PDO::FETCH_ASSOC);
            
            foreach ($subSubMenus as $subsub) {
                echo "      └─ {$subsub['title']} ({$subsub['node']})\n";
            }
        }
        echo "\n";
    }
    
    echo "=== 完成！===\n";
    echo "请刷新后台页面（Ctrl+F5）或重新登录查看菜单\n";
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

